package com.egg.basic.enums.pay;

import cn.hutool.core.util.ObjectUtil;
import com.egg.common.swagger.annotation.SwaggerDisplayAnnotation;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;


@Getter
@SwaggerDisplayAnnotation
@AllArgsConstructor
public enum ClientTypeEnum {


    APP("APP端", "app"),
    PC("PC端扫码", "pc"),
    WAP("WAP", "wap"),
    APPLET("小程序支付", "applet"),
    JSAPI("微信JSAPI", "jsapi"),
    MWEB("H5支付", "mweb"),
    ;


    private String message;
    private String code;


    public static Optional<ClientTypeEnum> findByCode(String code) {
        if (ObjectUtil.isEmpty(code)) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(o -> Objects.equals(code, o.getCode())).findAny();
    }

    public static ClientTypeEnum getByCode(String code) {
        if (ObjectUtil.isEmpty(code)) {
            throw new RuntimeException("编码无效");
        }
        Optional<ClientTypeEnum> clientType = Arrays.stream(values()).filter(o -> Objects.equals(code, o.getCode())).findAny();
        if (clientType.isPresent()) {
            return clientType.get();
        }
        throw new RuntimeException("编码无效");
    }

    @Override
    public String toString() {
        return message + ":" + code;
    }
}